<template>
	<view class="container">
		<view class="rent">
			<view class="rentTop">
				<view class="rentInfo">
					<view class="name">{{ room.room_address }}</view>
					<view class="group">{{ room.group_name }}</view>
				</view>
				<block v-for="item in dictList" :key="item.id">
					<view class="status" :class="'status' + detail.rent_status" v-if="detail.rent_status == item.item_code">
						{{ item.item_name }}
					</view>
				</block>
			</view>
			<view class="rentBtn">
				<view class="btn">
					水电记录
					<uni-icons type="right" size="18" color="#bbb"></uni-icons>
				</view>
				<view class="btn">
					开门权限
					<text>去管理</text>
					<uni-icons type="right" size="18" color="#bbb"></uni-icons>
				</view>
			</view>
		</view>
		<uni-segmented-control :current="navIndex" :values="detail.rent_status == 1 ? navList1 : navList2" style-type="text" @clickItem="onClickItem" />
		<view class="navBox0" v-if="navIndex == 0">
			<view class="detail">
				<view class="titleBox">
					<view class="title">入住信息</view>
					<view class="btn" v-if="detail.rent_status != 2" @click="editInfo">编辑</view>
				</view>
				<view class="info">
					<view class="label">
						<view class="name">姓名：</view>
						<view class="value">{{ detail.customer_info.customer_name }}</view>
					</view>
					<view class="label">
						<view class="name">联系方式：</view>
						<view class="value">{{ detail.customer_info.customer_mobile }}</view>
					</view>
					<view class="label">
						<view class="name">证件号码：</view>
						<view class="value">{{ detail.customer_info.customer_idcard }}</view>
					</view>
					<view class="label">
						<view class="name">入住周期：</view>
						<view class="value">{{ detail.start_date }} 至 {{ detail.end_date }}</view>
					</view>
				</view>
			</view>
			<view class="detail">
				<view class="titleBox">
					<view class="title">同住人信息</view>
					<view class="addTogether" v-if="detail.rent_status != 2" @click="addTogether(detail)">+添加同住人</view>
				</view>

				<view class="togetherList" v-if="detail.customer_list && detail.customer_list.length > 0">
					<view class="info" v-for="item in detail.customer_list" :key="item.id">
						<view class="label">
							<view class="name">姓名：</view>
							<view class="value">{{ item.customer_name }}</view>
						</view>
						<view class="label">
							<view class="name">联系方式：</view>
							<view class="value">{{ item.customer_mobile }}</view>
						</view>
						<view class="label">
							<view class="name">证件号码：</view>
							<view class="value">{{ item.customer_idcard }}</view>
						</view>
						<view class="btn" v-if="detail.rent_status != 2" @click="editTogether(item)">编辑</view>
					</view>
				</view>
			</view>
			<view class="detail">
				<view class="titleBox">
					<view class="title">备注</view>
					<view class="btn" v-if="detail.rent_status != 2" @click="editDesc(detail)">编辑</view>
				</view>
				<view class="desc" v-if="detail.rent_remark">
					{{ detail.rent_remark }}
				</view>
			</view>
			<view class="detail">
				<view class="titleBox">
					<view class="title">图片</view>
					<view class="btn" v-if="detail.rent_status != 2" @click="editImage(detail)">编辑</view>
				</view>
				<view class="media" v-if="images.length > 0">
					<uni-row :gutter="20">
						<uni-col :span="8" v-for="item in images" :key="item">
							<image :src="item" mode="aspectFill"></image>
						</uni-col>
					</uni-row>
				</view>
			</view>
			<view class="botBtn" v-if="detail.rent_status != 2">
				<button type="primary" @click="addHetong">添加合同</button>
				<button type="warn" @click="deleteRent">退房</button>
				<picker mode="date" @change="bindEnd" :start="detail.end_date" :value="detail.end_date">
					<button type="primary">续租</button>
				</picker>
			</view>
		</view>
		<view class="navBox1" v-else-if="navIndex == 1">
			<uni-table ref="payTable" :loading="payLoading">
				<view class="empty" v-if="payList.length < 1">
					<image class="image" src="@/static/img/empty.png" mode="aspectFit"></image>
				</view>
				<view v-else>
					<uni-tr>
						<uni-th>
							<view class="thBox">
								<view></view>
								<view class="num">
									<text class="income" v-if="payIncome != 0">收入：+{{ payIncome }}</text>
									<text class="payed" v-if="payPayed != 0">支出：{{ payPayed }}</text>
								</view>
							</view>
						</uni-th>
					</uni-tr>
					<uni-tr v-for="item in payList" :key="item.id">
						<uni-td>
							<view class="tdBox" @click="goBill(item)">
								<view class="left">
									<view class="name">{{ item.bill_title }}</view>
									<view class="time">{{ item.bill_date }}</view>
								</view>
								<view class="right">
									<view class="income" v-if="item.bill_type == 'income'">+{{ item.bill_money }}</view>
									<view class="payed" v-if="item.bill_type == 'payed'">{{ item.bill_money }}</view>
								</view>
							</view>
						</uni-td>
					</uni-tr>
				</view>
			</uni-table>
			<uni-pagination v-if="payTotal > payData.perpage" :total="payTotal" :pageSize="payData.perpage" :value="payData.page" @change="changePayPage" />
			<view class="botBtn" v-if="detail.rent_status != 2">
				<button type="primary" @click="addbill">添加记账</button>
			</view>
		</view>
		<view class="navBox2" v-else-if="navIndex == 2">
			<uni-table ref="paynotTable" :loading="paynotLoading" type="selection" @selection-change="selectionChange">
				<view class="empty" v-if="paynotList.length < 1">
					<image class="image" src="@/static/img/empty.png" mode="aspectFit"></image>
				</view>
				<view v-else>
					<uni-tr>
						<uni-th>
							<view class="thBox">
								<view>全选</view>
								<view class="num">
									<text class="income" v-if="paynotIncome != 0">收入：+{{ paynotIncome }}</text>
									<text class="payed" v-if="paynotPayed != 0">支出：-{{ paynotPayed }}</text>
								</view>
							</view>
						</uni-th>
					</uni-tr>
					<uni-tr v-for="item in paynotList" :key="item.id">
						<uni-td>
							<view class="tdBox" @click="clickBill(item)">
								<view class="left">
									<view class="name">{{ item.bill_title }}</view>
									<view class="time">{{ item.bill_date }}</view>
								</view>
								<view class="right">
									<view class="income" v-if="item.bill_type == 'income'">+{{ item.bill_money }}</view>
									<view class="payed" v-if="item.bill_type == 'payed'">-{{ item.bill_money }}</view>
								</view>
							</view>
						</uni-td>
					</uni-tr>
				</view>
			</uni-table>
			<uni-pagination v-if="paynotTotal > paynotData.perpage" :total="paynotTotal" :pageSize="paynotData.perpage" :value="paynotData.page" @change="changePaynotPage" />
			<view class="botBtn" v-if="detail.rent_status != 2">
				<button type="warn" @click="deletePaynot">批量删除</button>
				<button type="primary" @click="addbills">添加待收账单</button>
			</view>
		</view>
	</view>
</template>

<script>
import api from '@/api/api';
export default {
	data() {
		return {
			sn: null,
			dictList: [],
			detail: {},
			room: {},
			images: [],
			navList1: ['入住详情', '完成账单', '待收账单'],
			navList2: ['入住详情', '完成账单'],
			navIndex: 0,
			payLoading: false,
			payList: [],
			payTotal: 0,
			payIncome: 0,
			payPayed: 0,
			payData: {
				page: 1,
				perpage: 10,
				is_pay: 1
			},
			paynotLoading: false,
			paynotList: [],
			paynotTotal: 0,
			paynotIncome: 0,
			paynotPayed: 0,
			paynotData: {
				page: 1,
				perpage: 10,
				is_pay: 0
			},
			selectIndex: []
		};
	},
	onLoad(e) {
		this.sn = e.sn;
	},
	onShow() {
		if (this.navIndex == 0) {
			this.getDetail();
			this.getDict();
		} else if (this.navIndex == 1) {
			this.getPayList();
		} else if (this.navIndex == 2) {
			this.getPaynotList();
		}
	},
	methods: {
		onClickItem(e) {
			this.navIndex = e.currentIndex;
			if (this.navIndex == 1) {
				this.getPayList();
			} else if (this.navIndex == 2) {
				this.getPaynotList();
			}
		},
		editInfo() {
			uni.navigateTo({
				url: '/pages/details/info/rent?sn=' + this.sn + '&type=rent'
			});
		},
		editDesc(item) {
			uni.navigateTo({
				url: '/pages/details/desc?type=rent&sn=' + item.rent_sn + '&value=' + item.rent_remark
			});
		},
		editImage(item) {
			uni.setStorageSync('image', item.rent_images);
			uni.navigateTo({
				url: '/pages/details/info/image?type=rent&sn=' + item.rent_sn
			});
		},
		addTogether(item) {
			uni.navigateTo({
				url: '/pages/details/info/together?type=add&id=' + item.room_id + '&sn=' + item.rent_sn
			});
		},
		editTogether(item) {
			uni.navigateTo({
				url: '/pages/details/info/together?type=edit&id=' + item.id + '&name=' + item.customer_name + '&mobile=' + item.customer_mobile + '&idcard=' + item.customer_idcard
			});
		},
		addHetong() {
			uni.navigateTo({
				url: '/pages/add/hetong?detail=' + JSON.stringify(this.detail)
			});
		},
		addbill() {
			uni.navigateTo({
				url: '/pages/add/billPay?type=rent&sn=' + this.sn
			});
		},
		addbills() {
			uni.navigateTo({
				url: '/pages/add/billPayNot?type=rent&sn=' + this.sn
			});
		},
		goBill(item) {
			uni.navigateTo({
				url: '/pages/details/bill?id=' + item.id + '&type=1'
			});
		},
		clickBill(item) {
			const _this = this;
			uni.showActionSheet({
				itemList: ['完成收款', '催缴', '删除账单'],
				success: function (res) {
					if (res.tapIndex == 0) {
						uni.navigateTo({
							url: '/pages/add/setPayed?type=rent&id=' + item.id
						});
					} else if (res.tapIndex == 1) {
						console.log('催缴');
					} else if (res.tapIndex == 2) {
						uni.showModal({
							title: '提示',
							content: '确认删除该账单吗？',
							success: function (res) {
								if (res.confirm) {
									api.deleteBill({ bill_ids: item.id }).then((res) => {
										if (res.data.is_success) {
											uni.showToast({
												title: '删除成功！',
												icon: 'success'
											});
											_this.getPaynotList();
											_this.$refs.paynotTable.clearSelection();
										}
									});
								}
							}
						});
					}
				}
			});
		},
		deleteRent() {
			const _this = this;
			uni.showModal({
				title: '提示',
				content: '确定办理退房？',
				success: function (res) {
					if (res.confirm) {
						api.deteleRent({ rent_sn: _this.sn }).then((data) => {
							if (data.data.is_success) {
								uni.showToast({
									title: '操作成功！'
								});
								setTimeout(() => {
									uni.hideToast();
									uni.navigateBack();
								}, 1000);
							}
						});
					}
				}
			});
		},
		bindEnd(e) {
			let data = {
				rent_sn: this.sn,
				end_date: e.detail.value
			};
			api.rentDate(data).then((res) => {
				if (res.data.is_success) {
					uni.showToast({
						title: '续租成功！'
					});
					this.getDetail();
				}
			});
		},
		changePayPage(e) {
			this.payData.page = e.current;
			this.getPayList();
		},
		selectionChange(e) {
			this.selectIndex = e.detail.index;
		},
		deletePaynot() {
			if (this.selectIndex.length < 1) {
				uni.showToast({
					title: '未选中数据',
					icon: 'none'
				});
			} else {
				const _this = this;
				const ids = _this.selectIndex.map((i) => _this.paynotList[i].id).join();
				uni.showModal({
					title: '提示',
					content: '确认删除选中项？',
					success: function (res) {
						if (res.confirm) {
							api.deleteBill({ bill_ids: ids }).then((res) => {
								if (res.data.is_success) {
									uni.showToast({
										title: '删除成功！',
										icon: 'success'
									});
									_this.getPaynotList();
									_this.$refs.paynotTable.clearSelection();
								}
							});
						}
					}
				});
			}
		},
		changePaynotPage(e) {
			this.paynotData.page = e.current;
			this.getPaynotList();
		},
		getDetail() {
			api.rentDetail({ rent_sn: this.sn }).then((res) => {
				this.detail = res.data.info;
				this.images = res.data.info.rent_images == '' ? [] : res.data.info.rent_images.split(',');
				this.getRoomDetail();
			});
		},
		getPayList() {
			this.payData.rent_sn = this.sn;
			api.billList(this.payData).then((res) => {
				this.payList = res.data.bill_data.items;
				this.payTotal = res.data.bill_data.total;
				this.payIncome = res.data.total_money_income;
				this.payPayed = res.data.total_money_payed;
			});
		},
		getPaynotList() {
			this.paynotData.rent_sn = this.sn;
			api.billList(this.paynotData).then((res) => {
				this.paynotList = res.data.bill_data.items;
				this.paynotTotal = res.data.bill_data.total;
				this.paynotIncome = res.data.total_money_income;
				this.paynotPayed = res.data.total_money_payed;
			});
		},
		getRoomDetail() {
			api.roomDetail({ room_id: this.detail.room_id }).then((res) => {
				this.room = res.data.info;
			});
		},
		getDict() {
			api.getDict('rent_status').then((data) => {
				this.dictList = data.data;
			});
		}
	}
};
</script>

<style lang="scss">
.rent {
	background: #fff;
	padding: 20rpx 20rpx 0 20rpx;
	margin-bottom: 20rpx;
	.rentTop {
		border-bottom: 1rpx solid $uni-border-3;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-bottom: 20rpx;
		.rentInfo {
			.name {
				font-size: 30rpx;
				margin-bottom: 10rpx;
			}
			.group {
				font-size: 24rpx;
				color: $uni-secondary-color;
			}
		}
		.status {
			font-size: 24rpx;
			padding: 2rpx 10rpx;
			border-radius: 16rpx;
			&.status0 {
				background: $uni-primary-light;
				color: $uni-primary;
			}
			&.status1 {
				background: $uni-primary-light;
				color: $uni-primary;
			}
			&.status2 {
				background: $uni-info-light;
				color: $uni-info;
			}
		}
	}
	.rentBtn {
		display: flex;
		.btn {
			width: 50%;
			padding: 20rpx;
			font-size: 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			text {
				font-size: 26rpx;
				color: $uni-primary;
			}
			&:last-child {
				border-left: 1rpx solid $uni-border-3;
			}
		}
	}
}
.segmented-control {
	position: sticky;
	top: 0;
	background: #fff;
	z-index: 90;
}

.detail {
	padding: 20rpx;
	margin-bottom: 20rpx;
	background: #fff;
	.titleBox {
		display: flex;
		justify-content: space-between;
		align-items: center;
		.title {
			font-size: 30rpx;
			font-weight: bold;
		}
		.btn {
			font-size: 28rpx;
			color: $uni-primary;
		}
		.addTogether {
			font-size: 24rpx;
			padding: 4rpx 10rpx;
			border-radius: 20rpx;
			color: $uni-primary;
			background: $uni-primary-light;
		}
	}
	.info {
		margin-top: 20rpx;
		.label {
			display: flex;
			font-size: 28rpx;
			line-height: 2;
			.name {
				width: 160rpx;
				text-align: right;
				color: $uni-secondary-color;
			}
			.value {
				flex: 1;
			}
		}
	}
	.togetherList {
		.info {
			position: relative;
			border-top: 1rpx solid $uni-border-3;
			padding-top: 10rpx;
			&:first-child {
				border-top: none;
				padding-top: 0;
			}
			.btn {
				font-size: 28rpx;
				color: $uni-primary;
				position: absolute;
				right: 0;
				bottom: 20rpx;
			}
		}
	}
	.desc {
		margin-top: 20rpx;
		background: $uni-bg-color;
		padding: 10rpx;
		font-size: 28rpx;
		line-height: 42rpx;
		color: $uni-base-color;
	}
	.media {
		margin-top: 20rpx;
		.uni-row {
			width: 100%;
			.uni-col {
				height: 220rpx;
				overflow: hidden;
				margin-bottom: 20rpx;
				image {
					width: 100%;
					height: 220rpx;
				}
			}
		}
	}
}
.botBtn {
	margin-top: 100rpx;
	padding: 20rpx;
	display: flex;
	justify-content: space-between;
	padding-bottom: 100rpx;
}
.thBox {
	width: calc(100vw - 40rpx);
	display: flex;
	justify-content: space-between;
	.num {
		text {
			margin-left: 20rpx;
			font-size: 24rpx;
		}
		.income {
			color: #1ecb6b;
		}
		.payed {
			color: #ee5959;
		}
	}
}
.tdBox {
	display: flex;
	justify-content: space-between;
	align-items: center;
	.left {
		.name {
			font-size: 28rpx;
			color: #000;
			font-weight: bold;
		}
		.time {
			font-size: 24rpx;
			color: $uni-base-color;
		}
	}
	.right {
		font-size: 28rpx;
		.income {
			color: #1ecb6b;
		}
		.payed {
			color: #ee5959;
		}
	}
}
.navBox0 {
	.botBtn {
		button {
			width: 200rpx;
		}
	}
}
.navBox1 {
	.botBtn {
		button {
			width: 100%;
		}
	}
}
.navBox2 {
	.thBox {
		width: calc(100vw - 130rpx);
	}
	.botBtn {
		button {
			width: 300rpx;
		}
	}
}
</style>
